package constant.milk.cashbook.budgetstats;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.asset.MoneyActivity;
import constant.milk.cashbook.bean.BudgetData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.database.DbUpdateQuery;
import constant.milk.cashbook.tip.TipInfoActivity;
import constant.milk.cashbook.tip.TipMap;
import constant.milk.cashbook.util.MilkUtil;

public class BudgetUpdateActivity extends SuperActivity
{
	private static final int WEEKDAY_REQUEST_CODE = 0;
	private static final int MONTH_REQUEST_CODE = 1;
	private static final int YEAR_REQUEST_CODE = 2;
	
	private View weekdayView;
	private TextView weekdayTextView;
	
	private View monthView;
	private TextView monthTextView;
	
	private View yearView;
	private TextView yearTextView;
	
	private Button saveButton;
	
	private DBSelectQuery select;
	
	private int type;
	
	private BudgetData weekdayData;
	private BudgetData monthData;
	private BudgetData yearData;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.budget_update_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		weekdayView = findViewById(R.id.budgetUpdateWeekdayView);
		monthView = findViewById(R.id.budgetUpdateMonthView);
		yearView = findViewById(R.id.budgetUpdateYearView);
		
		weekdayTextView = (TextView) findViewById(R.id.budgetUpdateWeekdayTextView);
		monthTextView = (TextView) findViewById(R.id.budgetUpdateMonthTextView);
		yearTextView = (TextView) findViewById(R.id.budgetUpdateYearTextView);
		
		saveButton = (Button) findViewById(R.id.saveButton);
	}

	@Override
	protected void setOnClick()
	{
		weekdayView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MoneyActivity.class);
				intent.putExtra(MoneyActivity.MONEY_DATA, weekdayTextView.getText().toString());
				startActivityForResult(intent, WEEKDAY_REQUEST_CODE);
			}
		});
		
		monthView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MoneyActivity.class);
				intent.putExtra(MoneyActivity.MONEY_DATA, monthTextView.getText().toString());
				startActivityForResult(intent, MONTH_REQUEST_CODE);
			}
		});
		
		yearView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MoneyActivity.class);
				intent.putExtra(MoneyActivity.MONEY_DATA, yearTextView.getText().toString());
				startActivityForResult(intent, YEAR_REQUEST_CODE);
			}
		});
		
		saveButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (!checkInsert())
				{
					return;
				}
				
				if (setSaveData())
				{
					MilkUtil.showDialogFinish(superContext, "확인", "데이터 저장에 성공하였습니다.");
				}
				else
				{
					MilkUtil.showDialog(superContext, "실패", "잠시후 다시해 주세요.");
				}
			}
		});
	}

	@Override
	protected void setData()
	{
		select = new DBSelectQuery(superContext, superDb);
		
		Intent intent = getIntent();
		type = intent.getIntExtra(BudgetStatsActivity.TYPE, 0);
		
		if (type == 0)
		{
			setTitle("지출예산수정");
		}
		else if (type == 1)
		{
			setTitle("수입예산수정");
		}
		else if (type == 2)
		{
			setTitle("이동예산수정");
		}
		
		weekdayData = select.getBudget(type, 0);
		monthData = select.getBudget(type, 0);
		yearData = select.getBudget(type, 0);
		
		weekdayTextView.setText(MilkUtil.getWon(weekdayData.getMoney()));
		monthTextView.setText(MilkUtil.getWon(monthData.getMoney()));
		yearTextView.setText(MilkUtil.getWon(yearData.getMoney()));
	}
	
	private boolean checkInsert()
	{
		if (weekdayTextView.getText().toString().equals(""))
		{
			MilkUtil.showToast(superContext, "주별예산을 정확히 입력해 주세요.");
			return false;
		}
		
		if (weekdayTextView.getText().toString().equals(""))
		{
			MilkUtil.showToast(superContext, "월별예산을 정확히 입력해 주세요.");
			return false;
		}
		
		if (weekdayTextView.getText().toString().equals(""))
		{
			MilkUtil.showToast(superContext, "년별예산을 정확히 입력해 주세요.");
			return false;
		}
		
		return true;
	}
	
	private boolean setSaveData()
	{
		DbUpdateQuery query = null;
		
		try
		{
			query = new DbUpdateQuery(superContext, superDb);
			
			query.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			
			map.put("money", MilkUtil.getCommaDel(weekdayTextView.getText().toString()));
			
			query.setBudgetUpdate(map, weekdayData.getId());
			
			map.clear();
			
			map.put("money", MilkUtil.getCommaDel(monthTextView.getText().toString()));
			
			query.setBudgetUpdate(map, monthData.getId());
			
			map.clear();
			
			map.put("money", MilkUtil.getCommaDel(yearTextView.getText().toString()));
			
			query.setBudgetUpdate(map, yearData.getId());
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == WEEKDAY_REQUEST_CODE)
		{
			String str = data.getStringExtra(MoneyActivity.MONEY_DATA);
			weekdayTextView.setText(str);
		}
		else if (requestCode == MONTH_REQUEST_CODE)
		{
			String str = data.getStringExtra(MoneyActivity.MONEY_DATA);
			monthTextView.setText(str);
		}
		else if (requestCode == YEAR_REQUEST_CODE)
		{
			String str = data.getStringExtra(MoneyActivity.MONEY_DATA);
			yearTextView.setText(str);
		}
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(superContext, TipInfoActivity.class);
		
		if (type == BudgetStatsActivity.OUT)
		{
			intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_BUDGER_OUT_UPDATE);
		}
		else if (type == BudgetStatsActivity.IN)
		{
			intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_BUDGER_IN_UPDATE);
		}
		else if (type == BudgetStatsActivity.MOVE)
		{
			intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_BUDGER_MOVE_UPDATE);
		}
		startActivity(intent);
	}

}
